#include "libLUI_in.h"

static funcLUIOnButtonClickProc OnNoButtonClick ;
int OnNoButtonClick( struct LUIControl *control )
{
	struct LUIWindow	*window = NULL ;
	
	window = LUIGetWindowOfControl(control) ;
	LUISetWindowReturnCode( window , LUIWINDOW_RETURNCODE_NO );
	LUICloseWindow( window );
	
	return 0;
}

static funcLUIOnButtonClickProc OnYesButtonClick ;
int OnYesButtonClick( struct LUIControl *control )
{
	struct LUIWindow	*window = NULL ;
	
	window = LUIGetWindowOfControl(control) ;
	LUISetWindowReturnCode( window , LUIWINDOW_RETURNCODE_YES );
	LUICloseWindow( window );
	
	return 0;
}

static funcLUIOnButtonClickProc OnCancelButtonClick ;
int OnCancelButtonClick( struct LUIControl *control )
{
	struct LUIWindow	*window = NULL ;
	
	window = LUIGetWindowOfControl(control) ;
	LUISetWindowReturnCode( window , LUIWINDOW_RETURNCODE_CANCEL );
	LUICloseWindow( window );
	
	return 0;
}

static funcLUIOnButtonClickProc OnOkButtonClick ;
int OnOkButtonClick( struct LUIControl *control )
{
	struct LUIWindow	*window = NULL ;
	
	window = LUIGetWindowOfControl(control) ;
	LUISetWindowReturnCode( window , LUIWINDOW_RETURNCODE_OK );
	LUICloseWindow( window );
	
	return 0;
}

int LUIMessageBox( struct LUIWindowsManager *windows_manager , struct LUIWindow *parent_window , int styles , int properties , int buttons , wchar_t *title_text , wchar_t *message_text_format , ... )
{
	va_list			valist ;
	wchar_t			message_text[ 4096 ] ;
	
	struct LUIRectangle	window_rect ;
	struct LUIWindow	*window = NULL ;
	struct LUIControl	*label = NULL ;
	int			right ;
	struct LUIControl	*ok_button = NULL ;
	struct LUIControl	*cancel_button = NULL ;
	struct LUIControl	*yes_button = NULL ;
	struct LUIControl	*no_button = NULL ;
	int			return_code ;
	
	va_start( valist , message_text_format );
	memset( message_text , 0x00 , sizeof(message_text) );
	vswprintf( message_text , sizeof(message_text)/sizeof(wchar_t) , message_text_format , valist );
	va_end( valist );
	
	window_rect.width = 400 ;
	window_rect.height = 110 ;
	window_rect.left = ( windows_manager->screen_rect.width - window_rect.width ) / 2 ;
	window_rect.top = ( windows_manager->screen_rect.height - window_rect.height ) / 2 ;
	window = LUICreateWindow( windows_manager , parent_window , window_rect , title_text , LUIMESSAGEBOX_STYLES_DEFAULT , LUIMESSAGEBOX_PROPERTIES_DEFAULT ) ;
	if( window == NULL )
	{
		return -1;
	}
	
	label = LUICreateLabelControl( window , LUIRectangle(0,10,window_rect.width,30) , LUICONTROL_STYLES_DEFAULT|LUICONTROL_STYLE_TEXTALIGN_MIDDLE , LUICONTROL_PROPERTIES_DEFAULT , message_text ) ;
	if( label == NULL )
	{
		LUIDestroyWindow( window );
		return -1;
	}
	
	right = window_rect.width - 10 ;
	
	if( TEST_PROPERTY(buttons,LUIMESSAGEBOX_BUTTON_NO) )
	{
		right -= LUIBUTTON_WIDTH_DEFAULT ;
		no_button = LUICreateButtonControl( window , LUIRectangle(right,LUIWINDOW_TITLEBAR_HEIGHT_DEFAULT+LUIWINDOW_TITLEBAR_HEIGHT_DEFAULT,LUIBUTTON_WIDTH_DEFAULT,LUIBUTTON_HEIGHT_DEFAULT) , LUICONTROL_STYLES_DEFAULT , LUICONTROL_PROPERTIES_DEFAULT , L"NO" ) ;
		if( no_button == NULL )
		{
			LUIDestroyWindow( window );
			return -1;
		}
		LUISetOnButtonClickProc( no_button , & OnNoButtonClick );
		right -= 10 ;
	}
	
	if( TEST_PROPERTY(buttons,LUIMESSAGEBOX_BUTTON_YES) )
	{
		right -= LUIBUTTON_WIDTH_DEFAULT ;
		yes_button = LUICreateButtonControl( window , LUIRectangle(right,LUIWINDOW_TITLEBAR_HEIGHT_DEFAULT+LUIWINDOW_TITLEBAR_HEIGHT_DEFAULT,LUIBUTTON_WIDTH_DEFAULT,LUIBUTTON_HEIGHT_DEFAULT) , LUICONTROL_STYLES_DEFAULT , LUICONTROL_PROPERTIES_DEFAULT , L"YES" ) ;
		if( yes_button == NULL )
		{
			LUIDestroyWindow( window );
			return -1;
		}
		LUISetOnButtonClickProc( yes_button , & OnYesButtonClick );
		right -= 10 ;
	}
	
	if( TEST_PROPERTY(buttons,LUIMESSAGEBOX_BUTTON_CANCEL) )
	{
		right -= LUIBUTTON_WIDTH_DEFAULT ;
		cancel_button = LUICreateButtonControl( window , LUIRectangle(right,LUIWINDOW_TITLEBAR_HEIGHT_DEFAULT+LUIWINDOW_TITLEBAR_HEIGHT_DEFAULT,LUIBUTTON_WIDTH_DEFAULT,LUIBUTTON_HEIGHT_DEFAULT) , LUICONTROL_STYLES_DEFAULT , LUICONTROL_PROPERTIES_DEFAULT , L"CANCEL" ) ;
		if( cancel_button == NULL )
		{
			LUIDestroyWindow( window );
			return -1;
		}
		LUISetOnButtonClickProc( cancel_button , & OnCancelButtonClick );
		right -= 10 ;
	}
	
	if( TEST_PROPERTY(buttons,LUIMESSAGEBOX_BUTTON_OK) )
	{
		right -= LUIBUTTON_WIDTH_DEFAULT ;
		ok_button = LUICreateButtonControl( window , LUIRectangle(right,LUIWINDOW_TITLEBAR_HEIGHT_DEFAULT+LUIWINDOW_TITLEBAR_HEIGHT_DEFAULT,LUIBUTTON_WIDTH_DEFAULT,LUIBUTTON_HEIGHT_DEFAULT) , LUICONTROL_STYLES_DEFAULT , LUICONTROL_PROPERTIES_DEFAULT , L"OK" ) ;
		if( ok_button == NULL )
		{
			LUIDestroyWindow( window );
			return -1;
		}
		LUISetOnButtonClickProc( ok_button , & OnOkButtonClick );
		right -= 10 ;
	}
	
	windows_manager->p_modal_sub_window = window ;
	LUISetWindowReturnCode( window , LUIWINDOW_RETURNCODE_QUIT );
	
	return_code = LUIWindowsManagerLoop( windows_manager ) ;
	
	return return_code;
}
